package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PaymentOrder;
import com.jeequan.jeepay.core.entity.PaymentUsdtOrder;
import com.jeequan.jeepay.service.PaymentUsdtOrderService;
import com.jeequan.jeepay.service.mapper.PaymentUsdtOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PaymentUsdtOrder;
//import .mapper.PaymentUsdtOrderMapper;
//import .service.PaymentUsdtOrderService;
 /**
 * 回u订单表;(payment_usdt_order)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PaymentUsdtOrderServiceImpl implements PaymentUsdtOrderService {
    @Autowired
    private PaymentUsdtOrderMapper paymentUsdtOrderMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PaymentUsdtOrder queryById(Integer id){
        return paymentUsdtOrderMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param paymentUsdtOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PaymentUsdtOrder> paginQuery(PaymentUsdtOrder paymentUsdtOrder, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PaymentUsdtOrder> queryWrapper = new LambdaQueryWrapper<>();
        if(paymentUsdtOrder.getMemberId()!=null){
            queryWrapper.eq(PaymentUsdtOrder::getMemberId, paymentUsdtOrder.getMemberId());
        }
        if(paymentUsdtOrder.getState()!=null){
            queryWrapper.eq(PaymentUsdtOrder::getState, paymentUsdtOrder.getState());
        }
        if(StrUtil.isNotBlank(paymentUsdtOrder.getUsdtAddress())){
            queryWrapper.eq(PaymentUsdtOrder::getUsdtAddress, paymentUsdtOrder.getUsdtAddress());
        }
        if(StrUtil.isNotBlank(paymentUsdtOrder.getRemark())){
            queryWrapper.eq(PaymentUsdtOrder::getRemark, paymentUsdtOrder.getRemark());
        }
        //2. 执行分页查询
        Page<PaymentUsdtOrder> pagin = new Page<>(current , size , true);
        IPage<PaymentUsdtOrder> selectResult = paymentUsdtOrderMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param paymentUsdtOrder 实例对象
     * @return 实例对象
     */
    public PaymentUsdtOrder insert(PaymentUsdtOrder paymentUsdtOrder){
        paymentUsdtOrderMapper.insert(paymentUsdtOrder);
        return paymentUsdtOrder;
    }
    
    /** 
     * 更新数据
     *
     * @param paymentUsdtOrder 实例对象
     * @return 实例对象
     */
    public PaymentUsdtOrder update(PaymentUsdtOrder paymentUsdtOrder){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PaymentUsdtOrder> chainWrapper = new LambdaUpdateChainWrapper<PaymentUsdtOrder>(paymentUsdtOrderMapper);
        if(StrUtil.isNotBlank(paymentUsdtOrder.getUsdtAddress())){
            chainWrapper.set(PaymentUsdtOrder::getUsdtAddress, paymentUsdtOrder.getUsdtAddress());
        }
        if(StrUtil.isNotBlank(paymentUsdtOrder.getRemark())){
            chainWrapper.set(PaymentUsdtOrder::getRemark, paymentUsdtOrder.getRemark());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PaymentUsdtOrder::getId, paymentUsdtOrder.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(paymentUsdtOrder.getId());
        }else{
            return paymentUsdtOrder;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = paymentUsdtOrderMapper.deleteById(id);
        return total > 0;
    }
}